package icegps.com.pingdj.view;


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import icegps.com.pingdj.R;

/**
 * Created by 111 on 2017/12/29.
 */

public class Rssi extends View {
    private int yesColor;
    private int noColor;
    private Paint paint;
    private float rssi;
    private float paintWidth;
    private int rssiNum;

    public Rssi(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        yesColor = getResources().getColor(R.color.colorPrimary);
        noColor = Color.parseColor("#A0A0A0");
        paintWidth = getResources().getDimension(R.dimen.rssiWidth);
        paint = new Paint();
        paint.setStrokeWidth(paintWidth);
        paint.setStrokeCap(Paint.Cap.ROUND);
        paint.setColor(yesColor);
        rssiNum = 5;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int width = (getWidth());
        int height = (getHeight());

        for (int i = 1; i <= rssiNum; i++) {
            paint.setColor(noColor);
            if (rssi >= i) {
                paint.setColor(yesColor);
            }
            float startX = width / rssiNum * i - paintWidth / 2;
            float startY = height - (height / rssiNum * i);
            float stopX = width / rssiNum * i - paintWidth / 2;
            float stopY = getHeight();
            canvas.drawLine(startX, startY, stopX, stopY, paint);
        }
    }

    public void setRssi(int rssi) {
        if (rssi >= -40) {
            this.rssi = 5;
        } else if (rssi < -40 && rssi >= -60) {
            this.rssi = 4;
        } else if (rssi < -60 && rssi >= -70) {
            this.rssi = 3;
        } else if (rssi < -70 && rssi >= -80) {
            this.rssi = 2;
        } else if (rssi < -80 && rssi >= -90) {
            this.rssi = 1;
        } else {
            this.rssi = 0;
        }
        invalidate();
    }
}
